<!--
 * @Descripttion: 
 * @version: @ 1.0.0
 * @Author: 朱江洲
 * @Date: 2021-12-27 13:28:28
 * @LastEditors: 朱江洲
 * @LastEditTime: 2021-12-30 15:03:27
-->
<template>
  <div>
    <div class="img">
      <img src="../../assets/images/priceImg.jpg" alt="" />
      <div class="btn">
        <el-button type="primary" size="default" @click="buyNow"
          >立即购买</el-button
        >
      </div>
    </div>
    <el-dialog
      title="个人方案选择"
      :visible.sync="programmeShow"
      width="50%"
      @close="programmeClose"
      v-if="programmeShowEject"
    >
      <div class="basics" style="margin-right: 20px">购买方案</div>
      <div class="price">
        <div v-for="(item, index) in priceData" :key="index">
          <div
            class="price-item"
            @click="priceChi(item, index)"
            :class="index == active1 ? 'active1' : ''"
          >
            <div class="price-item-year">
              {{ item.yearNum }}
            </div>
          </div>
        </div>
      </div>
      <div class="zh"><span>当前账号：</span>{{ Account }}<span></span></div>
      <div class="sl">
        <span>证书数量：</span>
        <el-select v-model="numberPackage" @change="timeChange">
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="`${item.minNumber}-${item.maxNumber}`"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </div>
      <div class="time">
        <span>到期时间：{{ expireTime }}</span>
      </div>
      <div class="je">
        <span>订单金额：</span>
        <span class="amount">¥{{ amount }}</span>
      </div>
      <div>
        <div class="typeAtip">
          <div class="basics" style="margin-top: 30px; margin-right: 20px">
            支付方式
          </div>
          <div
            style="
              margin-top: 32px;
              font-size: 12px;
              font-family: PingFangSC-Regular, PingFang SC;
              font-weight: 400;
              color: #ff0000;
            "
          >
            注：支付成功之前请勿关闭页面！
          </div>
        </div>
        <div class="exainfoPay">
          <div class="payment-all">
            <div class="payment" @click="chat" :class="{ active: dialog == 0 }">
              <img src="@/assets/images/wechat.png" alt="" />
              微信支付
              <div class="payment-img" v-if="dialog == 0">
                <img src="@/assets/images/xiabiao.png" alt="" />
              </div>
            </div>
            <div class="payment" @click="ali" :class="{ active: dialog == 1 }">
              <img src="@/assets/images/paybaby.png" alt="" />
              支付宝支付
              <div class="payment-img" v-if="dialog == 1">
                <img src="@/assets/images/xiabiao.png" alt="" />
              </div>
            </div>
          </div>
          <div class="">
            <div class="payimg" v-if="dialog == 0">
              <div class="QRCode">
                <img :src="PayImgUrl" alt="" />
                <div class="footer">
                  <i class="iconfont icon-saoyisao"></i>
                  <div class="footer-zi">
                    <div>打开微信</div>
                    <div>扫一扫付款</div>
                  </div>
                </div>
              </div>
              <div class="wxS">
                <img src="@/assets/images/wxS.jpg" alt="" />
              </div>
            </div>
            <div class="payimg" v-else-if="dialog == 1">
              <div class="QRCode">
                <img :src="PayImgUrl" alt="" />
                <div class="footer">
                  <i class="iconfont icon-saoyisao"></i>
                  <div class="footer-zi">
                    <div>打开支付宝</div>
                    <div>扫一扫付款</div>
                  </div>
                </div>
              </div>
              <div class="wxS">
                <img src="@/assets/images/wxS.jpg" alt="" />
              </div>
            </div>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getWxQRCode,
  getWxOrder,
  closeWxOrder,
  getAliQRCode,
  getAliOrder,
  closeAliCode,
  getAccount,
  getNumber,
} from "@/api/purchase";
export default {
  props: {
    type: {
      type: Number,
      required: true,
    },
  },
  data() {
    return {
      Account: "",
      numberPackage: null,
      timePackage: 1,
      active1: 0,
      amount: 0,
      dialog: 0,
      timer: null,
      timer2: null,
      endTime: 0,
      expireTime: "",
      PayImgUrl: "",
      programmeShow: false,
      programmeShowEject: false,
      orderNumber: "",
      options: [],
      priceData: [
        {
          yearNum: "1年",
          type: 1,
        },
        {
          yearNum: "2年",
          type: 2,
        },
        {
          yearNum: "3年",
          type: 3,
        },
        {
          yearNum: "4年",
          type: 4,
        },
      ],
    };
  },
  created() {},
  methods: {
    // 获取人数
    getNumber() {
      getNumber({ type: this.type }).then((res) => {
        console.log(res);
        this.options = res.data;
        this.numberPackage = res.data[0].id;
        this.getAccount();
      });
    },
    // 人数变化
    timeChange() {
      this.getAccount();
    },
    // 获取价格
    getAccount() {
      getAccount({
        vipId: this.numberPackage,
        timePackage: this.timePackage,
        type: this.type,
      }).then((res) => {
        if (res.account !== this.Account) {
          this.Account = res.account;
        }
        this.expireTime = res.expireTime;
        this.amount = res.amount;
        if (this.dialog == 0) {
          this.chat();
        } else {
          this.ali();
        }
      });
    },
    // 选择年数
    priceChi(item, i) {
      this.active1 = i;
      this.timePackage = item.type;
      this.getAccount();
    },
    // 关闭个人方案选择
    programmeClose() {
      clearInterval(this.timer2);
      clearInterval(this.timer);
      this.dialog = Number;
      this.Account = "";
      this.numberPackage = null;
      this.timePackage = 1;
      this.active1 = 0;
      this.amount = 0;
      this.dialog = 0;
      this.programmeShowEject = false;
    },
    // 微信支付
    chat() {
      this.dialog = 0;
      clearInterval(this.timer2);
      clearInterval(this.timer);
      this.endTime = 0;
      let that = this;
      getWxQRCode({
        type: this.type,
        amount: this.amount,
      }).then((res) => {
        this.PayImgUrl = res.data.imgae;
        this.orderNumber = res.data.orderNumber;
        that.timer2 = setInterval(function () {
          that.endTime = that.endTime + 3;
          if (that.endTime == 60) {
            clearInterval(that.timer2);
            closeWxOrder({
              orderNumber: that.orderNumber,
            }).then((res) => {
              console.log(res);
            });
            return (that.endTime = 0);
          }
          getWxOrder({
            type: that.type,
            orderNumber: that.orderNumber,
            vipId: that.numberPackage,
            timePackage: that.timePackage,
            amount: that.amount,
          }).then((res) => {
            if (res.msg == "SUCCESS") {
              that.$message.success("支付成功");
              clearInterval(that.timer2);
              that.endTime = 0;
              that.programmeShow = false;
              that.$parent.onCheck();
            }
          });
        }, 3000);
      });
    },
    // 阿里支付
    ali() {
      clearInterval(this.timer);
      clearInterval(this.timer2);
      this.endTime = 0;
      let that = this;
      this.dialog = 1;
      getAliQRCode({
        type: this.type,
        amount: this.amount,
      }).then((res) => {
        this.PayImgUrl = res.data.imgae;
        this.orderNumber = res.data.orderNumber;
        that.timer = setInterval(function () {
          that.endTime = that.endTime + 3;
          if (that.endTime == 60) {
            clearInterval(that.timer);
            closeAliCode({
              schoolId: that.schoolForm.schoolName,
              orderNumber: that.orderNumber,
            }).then((res) => {
              console.log(res);
            });
            return (that.endTime = 0);
          }
          getAliOrder({
            type: that.type,
            orderNumber: that.orderNumber,
            vipId: that.numberPackage,
            timePackage: that.timePackage,
            amount: that.amount,
          }).then((res) => {
            if (res.msg == "TRADE_SUCCESS") {
              that.$message.success("支付成功");
              clearInterval(that.timer);
              that.endTime = 0;
              console.log(that.endTime);
              that.programmeShow = false;
              that.$parent.onCheck();
            }
          });
        }, 3000);
      });
    },
    buyNow() {
      this.programmeShowEject = true;
      this.$nextTick(function () {
        this.programmeShow = true;
        this.getNumber();
      });
    },
    onSubmit() {
      this.programmeShow = false;
    },
  },
};
</script>

<style lang="less" scoped>
.img {
  width: 1604px;
  position: relative;
  img {
    width: 100%;
  }
  .el-button {
    position: absolute;
    right: 10%;
    top: 3%;
  }
}
.allMain {
  /deep/ .el-dialog__body {
    height: 600px;
    overflow: auto;
  }
}
.main {
  width: 100%;
}
.main-content {
  display: flex;
  flex-wrap: wrap;

  width: 100%;
}
.content-all {
  width: 50%;
}
.basics {
  position: relative;
  font-size: 14px;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: #2b2b2b;
  margin-top: 10px;
}
.basics::before {
  content: "";
  position: absolute;
  left: -6px;
  top: 1px;
  width: 3px;
  height: 14px;
  background: #1267fa;
}
.basics-detail {
  margin-top: 20px;
  .basics-detail-info {
    margin-bottom: 10px;
    span {
      margin-left: 40px;
    }
  }
}
.exainfoPay {
  display: flex;
  .payment-all {
    .payment {
      margin-top: 16px;
      width: 235px;
      height: 71px;
      line-height: 71px;
      border-radius: 2px;
      border: 1px solid #d6d6d6;
      text-align: center;
      margin-right: 20px;
      font-size: 14px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #666666;
      position: relative;
      .payment-img {
        position: absolute;
        bottom: -27px;
        right: -1px;
      }
      img {
        width: 22px;
        height: 22px;
        vertical-align: middle;
        margin-bottom: 4px;
      }
    }
  }
}
.active {
  border: 1px solid #1267fa !important;
}
.load {
  margin-top: 20px;
}
.payimg {
  width: 414px;
  background-color: #f6f7f8;
  height: 215px;
  display: flex;
  justify-content: space-around;
  margin-top: 20px;
  padding-top: 22px;
  .QRCode {
    width: 139px;
    height: 175px;
    margin-left: 38px;
    background-color: #fff;
    img {
      width: 120px;
      height: 120px;
      margin-left: 10px;
      margin-top: 8px;
    }
    .footer {
      display: flex;
      justify-content: center;
      .footer-zi {
        font-size: 12px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #6a6d7c;
      }
      i {
        margin-right: 10px;
        color: #1267fa;
      }
    }
  }
  .wxS {
    width: 173px;
    height: 197px;
    img {
      width: 100%;
      height: 100%;
    }
  }
}
.comInfo {
  box-sizing: border-box;
  width: 414px;
  height: 284px;
  background-color: #f6f7f8;
  padding: 10px 20px;
  margin-top: 10px;
}
/deep/ .el-dialog__footer {
  .downTip {
    width: 100%;
    display: flex;
    justify-content: space-between;
  }
}
.typeAtip {
  display: flex;
}
.priceall {
  font-size: 18px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 600;
  color: #ff0000;
}
.lastInfo {
  display: flex;
  .uploadlast {
    display: flex;
    align-items: flex-start;
    /deep/ .el-upload-list__item {
      margin-top: 0 !important;
      line-height: 1.1;
      .el-upload-list__item-name {
        width: 200px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }
  }
}
.price {
  margin-top: 20px;
  display: flex;
}
.price-item {
  margin-right: 40px;
  width: 155px;
  height: 88px;
  background: #ffffff;
  border-radius: 8px;
  border: 1px solid #dcdcdc;
  padding: 20px;
  .price-item-yea {
    font-size: 14px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #2b2b2b;
  }
  .price-item-price {
    margin-top: 10px;
    font-size: 16px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: #fc6457;
  }
}
.active1 {
  box-shadow: 0px 3px 8px 0px rgba(18, 103, 250, 0.14) !important;
  border-radius: 8px !important;
  border: 1px solid #1267fa !important;
}
.zh {
  margin-top: 20px;
}
.amount {
  font-size: 16px;
  color: #fc6457;
}
.sl {
  margin-top: 20px;
}
.time {
  margin-top: 20px;
}
.je {
  margin-top: 20px;
}
</style>